package com.maslyk.invitation.jsfcontroller;

import java.util.ArrayList;
import java.util.List;

import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import org.apache.myfaces.trinidad.component.core.data.CoreTable;
import org.apache.myfaces.trinidad.context.RequestContext;

import com.maslyk.configuration.DataCache;
import com.maslyk.invitation.BeanService;
import com.maslyk.invitation.PageTools;
import com.maslyk.invitation.entitybean.Text;
import com.maslyk.invitation.enums.LocalNames;
import com.maslyk.invitation.enums.ViewIdentityEnum;
import com.maslyk.invitation.exception.LogicalException;
import com.maslyk.invitation.service.PersistenceService;
import com.maslyk.invitation.to.SessionDataTO;

public class TextController
{
    private CoreTable textListDataTable;
    
	private PersistenceService persistenceService;

    public TextController()
    {
    }

	public List<Text> getTextList()
    {   
        return persistenceService.getTextList();
    }

    public List<SelectItem> getTextItemList()
    {
        List<SelectItem> items = new ArrayList<SelectItem>();

        for(Text text : getTextList())
        {
            SelectItem item = new SelectItem();
            item.setValue(text.getId());
            item.setLabel(text.getCode() +": "+text.getContent());
            item.setDescription(text.getDescription());
            items.add(item);
        }
        return items;
    }

	public String createText() throws Exception
    {
        Text text = (Text) BeanService.getSessionScopeBean(Text.getBeanName());

        persistenceService.saveText(text);

        SessionDataTO.setCurrentView(ViewIdentityEnum.TEXT_LIST);
        BeanService.putSessionScopeBean(Text.getBeanName(), null);
        return null;
    }

    public String updateText()
    {
        if(textListDataTable.getSelectedRowKeys().getSize() == 0)
            return null;

        if(textListDataTable.getSelectedRowKeys().getSize() > 1)
        {
            PageTools.addMessage(new LogicalException("Zaznaczono wiecej niz jeden text!"));
            return null;
        }

        textListDataTable.setRowKey(textListDataTable.getSelectedRowKeys().iterator().next());
        BeanService.putSessionScopeBean(Text.getBeanName(), textListDataTable.getRowData());
        SessionDataTO.setCurrentView(ViewIdentityEnum.TEXT_FORM);
        return null;
    }

	public String removeText() throws Exception
    {
		Text text = (Text)textListDataTable.getRowData();
        persistenceService.removeText(text);
        SessionDataTO.setCurrentView(ViewIdentityEnum.TEXT_LIST);
        return null;
    }

    public String selectText()
    {
        return null;
    }

    public CoreTable getTextListDataTable()
    {
        return textListDataTable;
    }

    public void setTextListDataTable(CoreTable textListDataTable)
    {
        this.textListDataTable = textListDataTable;
    }

    public void changeTextType(ValueChangeEvent event)
    {
        Integer textType = (Integer) event.getNewValue();

        if (textType == null)
        {
            return;
        }

        Text text = (Text) BeanService.getSessionScopeBean(Text.getBeanName());

        switch (textType.intValue())
        {
            case 1:
                text.setCode("C-");
                break;

            case 2:
                text.setCode("P-");
                break;

            case 3:
                text.setCode("T-");
                break;

            case 4:
                text.setCode("W-");
                break;

            case 5:
                text.setCode("S-");
                break;

            case 6:
                text.setCode("U-");
                break;

            case 7:

                text.setCode("A-");
                break;

            case 8:
                text.setCode("H-");
                break;
            default:
                text.setCode(null);
        }
    }
    
    public String addTextToInvitation()
    {
        List<Text> textList = new ArrayList<Text>();

        for (Object key : textListDataTable.getSelectedRowKeys())
        {
            textListDataTable.setRowKey(key);
            textList.add((Text) textListDataTable.getRowData());
        }

        RequestContext.getCurrentInstance().returnFromDialog(textList, null);
        return null;
    }
	
	public void setPersistenceService(PersistenceService persistenceService) {
		this.persistenceService = persistenceService;
	}
}
